﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using ApartmentBillsManager.Start.Annotations;
using ApartmentBillsManager.Start.Common;

namespace ApartmentBillsManager.Start.Services
{
    class CreateEditRoommate : INotifyPropertyChanged
    {
        #region Data Members
        private bool _isDateOfLeavingRelevat;
        private string _name;
        private DateTime _dateOfEntrance;
        private DateTime _dateOfLeaving;
        private MessageBoxResult _messageBoxResult;
        #endregion

        #region Properties
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("CanAddRoommate");
            }
        }

        public DateTime DateOfEntrance
        {
            get { return _dateOfEntrance; }
            set
            {
                _dateOfEntrance = value;
                OnPropertyChanged("CanAddRoommate");
            }
        }

        public DateTime DateOfLeaving
        {
            get { return _dateOfLeaving; }
            set
            {
                _dateOfLeaving = value;
                OnPropertyChanged("DateOfLeaving");
                OnPropertyChanged("CanAddRoommate");
            }
        }

        public bool IsDateOfLeavingRelevat
        {
            get { return _isDateOfLeavingRelevat; }
            set
            {
                _isDateOfLeavingRelevat = value;
                OnPropertyChanged("IsDateOfLeavingRelevat");

                if (_isDateOfLeavingRelevat)
                    DateOfLeaving = DateOfEntrance.AddDays(1);
                else
                    _dateOfLeaving = DateTime.MaxValue.Date;
            }
        }

        public bool CanAddRoommate
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Name))
                    return false;

                if (DateOfLeaving < DateOfEntrance)
                    return false;

                return true;
            }
        }
        #endregion

        #region Public Methods
        public Roommate CreateNewRoommate()
        {
            Name = string.Empty;
            DateOfEntrance = DateTime.Now;
            IsDateOfLeavingRelevat = false;
            DateOfLeaving = DateTime.Now.AddDays(1);

            OpenWindow();

            if (!IsDateOfLeavingRelevat)
                DateOfLeaving = DateTime.MaxValue;

            return _messageBoxResult == MessageBoxResult.OK ? new Roommate(Name, DateOfEntrance, DateOfLeaving) : null;
        }

        public Roommate EditExitingRoommate(Roommate roommate)
        {
            Name = roommate.Name;
            DateOfEntrance = roommate.DateOfEntrance;
            DateOfLeaving = roommate.DateOfLeaving;

            if (DateOfLeaving.Date == DateTime.MaxValue.Date)
                IsDateOfLeavingRelevat = false;
            else
                IsDateOfLeavingRelevat = true;

            OpenWindow();

            if (!IsDateOfLeavingRelevat)
                DateOfLeaving = DateTime.MaxValue;

            return _messageBoxResult == MessageBoxResult.OK ? new Roommate(Name, DateOfEntrance, DateOfLeaving) : null;
        }
        #endregion

        #region Private Methods
        private void OpenWindow()
        {
            var createNewRoommateWindow = new Window
                {
                    Content = this,
                    Title = "Create New Roommate",
                    SizeToContent = SizeToContent.WidthAndHeight
                };

            createNewRoommateWindow.ShowDialog();
        }

        private void AddRoommateButton(object obj)
        {
            _messageBoxResult = MessageBoxResult.OK;

            CloseWindow();
        }

        private void CancelRoommateButton(object obj)
        {
            _messageBoxResult = MessageBoxResult.Cancel;

            CloseWindow();
        }

        private void CloseWindow()
        {
            Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive).Close();
        }
        #endregion


        #region Commands
        public ICommand AddRoommateButtonCommand
        {
            get { return new DelegateCommand(AddRoommateButton); }
        }

        public ICommand CancelRoommateButtonCommand
        {
            get { return new DelegateCommand(CancelRoommateButton); }
        }
        #endregion

        #region NotifyPropertyChanegd
        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}
